<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    <title>php5.3以后的foreach的&amp;符号不建议使用 | NewThink - Blog | 思考，再思考，前进，再前进……</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="">
    <meta name="description" content="1、原因有时候会出现奇怪的问题， 有时候上边的foreach没问题，下边的却出现了问题！究其原因一句话解释：

在 foreach($arr as &amp;amp;$value) 数组遍历到最后，引用关系并没有断开

什么原因呢？白羽的一篇文章详细解释了这一切，文章地址:http://note.youdao.com/share/?id=246baa8f946c354d9ddee002dd0e3df6&amp;a">
<meta property="og:type" content="article">
<meta property="og:title" content="php5.3以后的foreach的&符号不建议使用">
<meta property="og:url" content="http://newthink.cc/2017/02/10/php5.3以后的foreach的&符号不建议使用/index.html">
<meta property="og:site_name" content="NewThink - Blog">
<meta property="og:description" content="1、原因有时候会出现奇怪的问题， 有时候上边的foreach没问题，下边的却出现了问题！究其原因一句话解释：

在 foreach($arr as &amp;amp;$value) 数组遍历到最后，引用关系并没有断开

什么原因呢？白羽的一篇文章详细解释了这一切，文章地址:http://note.youdao.com/share/?id=246baa8f946c354d9ddee002dd0e3df6&amp;a">
<meta property="og:updated_time" content="2017-02-10T09:51:15.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="php5.3以后的foreach的&符号不建议使用">
<meta name="twitter:description" content="1、原因有时候会出现奇怪的问题， 有时候上边的foreach没问题，下边的却出现了问题！究其原因一句话解释：

在 foreach($arr as &amp;amp;$value) 数组遍历到最后，引用关系并没有断开

什么原因呢？白羽的一篇文章详细解释了这一切，文章地址:http://note.youdao.com/share/?id=246baa8f946c354d9ddee002dd0e3df6&amp;a">
    
        <link rel="alternative" href="/atom.xml" title="NewThink - Blog" type="application/atom+xml">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="/css/style.css?v=1.4.7">
    <script>window.lazyScripts=[]</script>
</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">杨红伟</h5>
          <a href="mailto:634206017@qq.com" title="634206017@qq.com" class="mail">634206017@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/yscoder" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="http://www.weibo.com/ysweb" target="_blank" >
                <i class="icon icon-lg icon-weibo"></i>
                Weibo
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/custom"  >
                <i class="icon icon-lg icon-link"></i>
                测试
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">php5.3以后的foreach的&amp;符号不建议使用</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">php5.3以后的foreach的&amp;符号不建议使用</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-02-10T09:50:47.965Z" itemprop="datePublished" class="page-time">
  2017-02-10
</time>


            
        </h5>
    </div>

    

</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#1、原因"><span class="post-toc-number">1.</span> <span class="post-toc-text">1、原因</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#2、php5-3以前的写法："><span class="post-toc-number">2.</span> <span class="post-toc-text">2、php5.3以前的写法：</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#3、php5-3以后的写法："><span class="post-toc-number">3.</span> <span class="post-toc-text">3、php5.3以后的写法：</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#4、示例如下："><span class="post-toc-number">4.</span> <span class="post-toc-text">4、示例如下：</span></a></li></ol>
        </nav>
    </aside>
    
<article id="post-php5.3以后的foreach的&amp;符号不建议使用"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">php5.3以后的foreach的&符号不建议使用</h1>
        <div class="post-meta">
            <time class="post-time" title="2017年02月10日 17:50" datetime="2017-02-10T09:50:47.965Z"  itemprop="datePublished">2017-02-10</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h2 id="1、原因"><a href="#1、原因" class="headerlink" title="1、原因"></a>1、原因</h2><p>有时候会出现奇怪的问题， 有时候上边的foreach没问题，下边的却出现了问题！究其原因一句话解释：</p>
<blockquote>
<p>在 <code>foreach($arr as &amp;$value)</code> 数组遍历到最后，引用关系并没有断开</p>
</blockquote>
<p>什么原因呢？白羽的一篇文章详细解释了这一切，文章地址:<a href="http://note.youdao.com/share/?id=246baa8f946c354d9ddee002dd0e3df6&amp;type=note#/" target="_blank" rel="external">http://note.youdao.com/share/?id=246baa8f946c354d9ddee002dd0e3df6&amp;type=note#/</a></p>
<h2 id="2、php5-3以前的写法："><a href="#2、php5-3以前的写法：" class="headerlink" title="2、php5.3以前的写法："></a>2、php5.3以前的写法：</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">foreach ($arr as $key =&gt; &amp;$value)&#123;</div><div class="line">    $value[&apos;sex&apos;] = &apos;man&apos;;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="3、php5-3以后的写法："><a href="#3、php5-3以后的写法：" class="headerlink" title="3、php5.3以后的写法："></a>3、php5.3以后的写法：</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">foreach ($arr as $key =&gt; $value)&#123;</div><div class="line">    $value[$key][&apos;sex&apos;] = &apos;man&apos;;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>或者<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">foreach ($arr as $key =&gt; &amp;$value)&#123;</div><div class="line">     $value[&apos;sex&apos;] = &apos;man&apos;;</div><div class="line">&#125;</div><div class="line">unset($value);</div></pre></td></tr></table></figure></p>
<h2 id="4、示例如下："><a href="#4、示例如下：" class="headerlink" title="4、示例如下："></a>4、示例如下：</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line">&lt;?php</div><div class="line">$arr = array(</div><div class="line">    array(</div><div class="line">        &quot;id&quot;=&gt;&quot;1&quot;,</div><div class="line">        &quot;value&quot;=&gt;21</div><div class="line">    ),</div><div class="line">    array(</div><div class="line">        &quot;id&quot;=&gt;&quot;2&quot;,</div><div class="line">        &quot;value&quot;=&gt;22</div><div class="line">    ),</div><div class="line">    array(</div><div class="line">        &quot;id&quot;=&gt;&quot;4&quot;,</div><div class="line">        &quot;value&quot;=&gt;23</div><div class="line">    )</div><div class="line"></div><div class="line">);</div><div class="line"></div><div class="line">//比如增加一个 sex 的值</div><div class="line">foreach ($arr as $key =&gt; &amp;$value)&#123;</div><div class="line">    $value[&apos;sex&apos;] = &apos;man&apos;;</div><div class="line">&#125;</div><div class="line">//print_r($arr);</div><div class="line"></div><div class="line">foreach ($arr as $key =&gt; $value) &#123;</div><div class="line">    $arr[$key][&apos;sex&apos;] = &apos;man&apos;;</div><div class="line">&#125;</div><div class="line">//print_r($arr);</div></pre></td></tr></table></figure>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-02-10T09:51:15.000Z" itemprop="dateUpdated">2017年2月10日 17:51</time>
</span><br>


        这里写留言或版权声明：<a href="/2017/02/10/php5.3以后的foreach的&amp;符号不建议使用/" target="_blank" rel="external">http://newthink.cc/2017/02/10/php5.3以后的foreach的&amp;符号不建议使用/</a>
    </div>
    <footer>
        <a href="http://newthink.cc">
            <img src="/img/avatar.jpg" alt="杨红伟">
            杨红伟
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            

            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://newthink.cc/2017/02/10/php5.3以后的foreach的&符号不建议使用/&title=《php5.3以后的foreach的&符号不建议使用》 — NewThink - Blog&pic=http://newthink.cc/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://newthink.cc/2017/02/10/php5.3以后的foreach的&符号不建议使用/&title=《php5.3以后的foreach的&符号不建议使用》 — NewThink - Blog&source=之所以叫 NewThink，是我们想以更为先进的方式来考虑！" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://newthink.cc/2017/02/10/php5.3以后的foreach的&符号不建议使用/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《php5.3以后的foreach的&符号不建议使用》 — NewThink - Blog&url=http://newthink.cc/2017/02/10/php5.3以后的foreach的&符号不建议使用/&via=http://newthink.cc" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://newthink.cc/2017/02/10/php5.3以后的foreach的&符号不建议使用/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/02/13/列表页及上拉加载、下拉刷新/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">列表页及上拉加载、下拉刷新</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/02/10/foreach中的&符号baiyu/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">php foreach 使用&amp;(与运算符)引用赋值要注意的问题</h4>
      </a>
    </div>
  
</nav>



    

<div class="comments" id="comments">
    <div class="ds-thread" data-thread-key="php5.3以后的foreach的&符号不建议使用" data-title="php5.3以后的foreach的&符号不建议使用" data-url="http://newthink.cc/2017/02/10/php5.3以后的foreach的&符号不建议使用/"></div>
</div>
<script>
lazyScripts.push('//cdn.bootcss.com/marked/0.3.6/marked.min.js');

var duoshuoQuery = {short_name:'ysblog', theme: 'none'};
lazyScripts.push('/js/embed.min.js?v=1.4.7');


</script>







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <ul class="reward-items">
        
        <li>
            <img src="/img/wechat.jpg" title="微信打赏二维码" alt="微信打赏二维码">
            <p>微信</p>
        </li>
        

        
        <li>
            <img src="/img/alipay.jpg" title="支付宝打赏二维码" alt="支付宝打赏二维码">
            <p>支付宝</p>
        </li>
        
    </ul>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            <span>博客内容遵循 <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p>
            <span>Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a></span>
            <span>NewThink - Blog &copy; 2015 - 2017</span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://newthink.cc/2017/02/10/php5.3以后的foreach的&符号不建议使用/&title=《php5.3以后的foreach的&符号不建议使用》 — NewThink - Blog&pic=http://newthink.cc/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://newthink.cc/2017/02/10/php5.3以后的foreach的&符号不建议使用/&title=《php5.3以后的foreach的&符号不建议使用》 — NewThink - Blog&source=之所以叫 NewThink，是我们想以更为先进的方式来考虑！" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://newthink.cc/2017/02/10/php5.3以后的foreach的&符号不建议使用/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《php5.3以后的foreach的&符号不建议使用》 — NewThink - Blog&url=http://newthink.cc/2017/02/10/php5.3以后的foreach的&符号不建议使用/&via=http://newthink.cc" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://newthink.cc/2017/02/10/php5.3以后的foreach的&符号不建议使用/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };



</script>

<script src="/js/main.min.js?v=1.4.7"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="/js/search.min.js?v=1.4.7" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>


</body>
</html>
